<?php
    
    session_start();
    

    function getUserNamePage(){
        if (isset($_SESSION['username']))
	{
		$login = true;
	}
	else{
		$login = false;
		//header("Location: ".$CFG->url."home.php");
	}
	$loguname = $_SESSION['username'];
	$username = $_GET["un"];
	if ($username==NULL&&$login){
		$username=$loguname;
	}
        return $username;
    }

    $user = $_SESSION['username'];
    require_once('php/database.php');


    database::query("INSERT INTO thread (JudulThread) VALUES ('".$_POST['judularea']."')");
    //echo 'judul nih '.$_POST['judularea'].'<br/>';

    $hasil = database::query("SELECT * FROM thread WHERE JudulThread = '".$_POST['judularea']."'");

    if(mysql_num_rows($hasil)>0){
        $baris = mysql_fetch_row($hasil);
        $idthreadsaatini = $baris[0];
        //echo 'id nih '.$idthreadsaatini.'<br/>';
    }

    //echo 'pesan nih '.$_POST['pesanarea'].'<br/>';
    database::query("INSERT INTO message (Pengirim, Pesan, IDThread, TanggalKirim)
        VALUES ('".$user."', '".$_POST['pesanarea']."', '".$idthreadsaatini."', CURRENT_TIMESTAMP )");

    database::query("INSERT INTO messagetarget (IDThread, Username)
            VALUES ('".$idthreadsaatini."','".$user."')");


    $jum = $_POST['count'];
    $arrayusr = $_POST['stringuser'];
    $a = 1;

    for ($i = 1; $i < strlen($arrayusr); ++$i){
        if ($arrayusr[$i] == ' '){
            $target = substr($arrayusr, $a, $i-$a);

            database::query("INSERT INTO messagetarget (IDThread, Username)
                VALUES ('".$idthreadsaatini."','".$target."')");
            //echo 'target nih '.$target.'<br/>';

            $a = $i+1;

            //echo 'target = '.$target.'asdasd<br/>';
            //echo "INSERT INTO messagetarget (IDThread, Username) VALUES ('".$idthreadsaatini."','".$target."')";

            /*mysql_query("INSERT INTO messagetarget (IDThread, Username)
            VALUES ('".$idthreadsaatini."','".$target."')");*/

        }
    }
    header("Location: message.php?un=".$user."");
?>
